﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ThuChi
{
    class Utilities
    {
        public static string GetMd5Hash(string input)
        {
            MD5 md5Hash = MD5.Create();
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
        public static bool VerifyMd5Hash(string input, string hash)
        {
            string hashOfInput = GetMd5Hash(input);
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;
            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static int GetMaQuyen(int MaTaiKhoan)
        {
            DCThuChiDataContext dc = new DCThuChiDataContext();
            var truyvan = (from tk in dc.TaiKhoans
                           where tk.MaTaiKhoan == MaTaiKhoan && tk.TinhTrang == true
                           select new
                           {
                               tk.MaQuyen
                           });
            if (truyvan.Count() == 0) return 0;
            return truyvan.First().MaQuyen;
        }

        public static String GetMatKhau(int MaTaiKhoan)
        {
            DCThuChiDataContext dc = new DCThuChiDataContext();
            var truyvan = (from tk in dc.TaiKhoans
                           where tk.MaTaiKhoan == MaTaiKhoan && tk.TinhTrang == true
                           select new
                           {
                               tk.MatKhau
                           });
            if (truyvan.Count() == 0) return "";
            return truyvan.First().MatKhau;
        }

        public static TaiKhoan GetTaiKhoanById(int MaTaiKhoan)
        {
            DCThuChiDataContext dc = new DCThuChiDataContext();
            try
            {
                return dc.TaiKhoans.Single(tk => tk.MaTaiKhoan == MaTaiKhoan && tk.TinhTrang == true);
            }
            catch //(System.Exception ex)
            {
                return null;
            }

        }
    }
}
